package com.fare.taluo.core.domain.crawl.impl;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.dayatang.hibernate.EntityRepositoryHibernate;
import com.fare.taluo.core.domain.crawl.FdcCrawlStatisticsForFlightLineEntity;
import com.fare.taluo.core.domain.crawl.FdcCrawlStatisticsForFlightLineEntityRepository;
import com.fare.taluo.util.SpringUtil;


@Repository("fdcCrawlStatisticsForFlightLineEntityRepository")
public class FdcCrawlStatisticsForFlightLineEntityRepositoryImpl extends EntityRepositoryHibernate implements FdcCrawlStatisticsForFlightLineEntityRepository{
	private JdbcTemplate jdbctemp=(JdbcTemplate) SpringUtil.getBean("jdbcTemplate");
	@SuppressWarnings("unchecked")
	@Override
	public List<FdcCrawlStatisticsForFlightLineEntity> findForFlightLineEntity(String fromCityCode, String toCityCode) {
		List<FdcCrawlStatisticsForFlightLineEntity> list =new ArrayList<FdcCrawlStatisticsForFlightLineEntity>();
		/*
		//hibernate查询
		StringBuffer jpql =new StringBuffer("from FdcCrawlStatisticsForFlightLineEntity plane where 1=1 ");
		List<Object> conditionVals = new ArrayList<Object>();
		if(fromCityCode !=null && !fromCityCode.equals("")){
			jpql.append(" and plane.fromCity =? ");
			conditionVals.add(fromCityCode);			
		}
		if(toCityCode !=null && !toCityCode.equals("")){
			jpql.append(" and plane.toCity =? ");
			conditionVals.add(toCityCode);			
		}
		list =super.find(jpql.toString(),conditionVals.toArray(),FdcCrawlStatisticsForFlightLineEntity.class);
		*/
		
		//jdbc查询
		StringBuffer jpql =new StringBuffer("select * from fdc_crawl_statistics_flightline where 1=1 ");
		if(fromCityCode !=null && !fromCityCode.equals("")){
			jpql.append(" and from_city = '"+fromCityCode+"' ");
		}
		if(toCityCode !=null && !toCityCode.equals("")){
			jpql.append(" and to_city = '"+toCityCode+"' ");
		}
		list=(List<FdcCrawlStatisticsForFlightLineEntity>)jdbctemp.query(jpql.toString(), new ItemMapper());
		
		return list;
	}
	
	@SuppressWarnings({ "serial", "rawtypes" })
	protected class ItemMapper implements RowMapper ,Serializable  {

		@Override
		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
			FdcCrawlStatisticsForFlightLineEntity item=new FdcCrawlStatisticsForFlightLineEntity();
			item.setId(rs.getString("id"));
			item.setCreateTime(rs.getDate("create_time"));
			item.setFromCity(rs.getString("from_city"));
			item.setFromCityName(rs.getString("from_city_name"));
			item.setToCity(rs.getString("to_city"));
			item.setToCityName(rs.getString("to_city_name"));
			item.setGrabStartDate(rs.getDate("grab_start_date"));
			item.setGrabEndDate(rs.getDate("grab_end_date"));
			item.setGrabTotalSource(rs.getLong("grab_total_source"));
			return item;
		}
		
	}
	
	
}
